package cn.btks.resultintegration.util; /**
 * Created by windows10 on 2017/5/17.
 */

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.*;

/**
 * bird on 2016/12/14.
 */
public class HttpUtil {

    public static String getUrlBuilder(String url, Map param){

        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        Set<String> keys = param.keySet();
        for (String name : keys) {
            Object value = param.get(name);
            BasicNameValuePair nameValuePair = new BasicNameValuePair(name, value.toString());
            params.add(nameValuePair);
        }
        String queryString = URLEncodedUtils.format(params, "UTF-8");
        return url + "?" + queryString;
    }

    public static String httpGet(String url){
        HttpGet get = new HttpGet(url);
        HttpClient client = HttpClients.createDefault();
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity,"utf-8");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String httpGet(String url,Map map){
        HttpGet get = new HttpGet(getUrlBuilder(url,map));
        HttpClient client = HttpClients.createDefault();
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity,"utf-8");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static UrlEncodedFormEntity httpPostParameters(Map<String, String> map) throws HttpException {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        Set<String> keys = map.keySet();
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            String name = (String) iterator.next();
            String value = map.get(name);
            BasicNameValuePair nameValuePair = new BasicNameValuePair(name, value);
            params.add(nameValuePair);
        }
        try {
            return new UrlEncodedFormEntity(params, HTTP.UTF_8);
        } catch (UnsupportedEncodingException e) {
            throw new HttpException("httpPostParameters  error");
        }
    }
    public static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";


    public static String httpPost( String url, Map<String,String> map){
        int codeStatus = 500;
        String result = "";
        HttpClient client = HttpClients.createDefault();
        try {
            URI uri = new URI(url);
            HttpPost post = new HttpPost(uri);
            post.setEntity(httpPostParameters(map));
            post.setHeader(HTTP.USER_AGENT,USER_AGENT);
            HttpResponse response = client.execute(post);
            codeStatus = response.getStatusLine().getStatusCode();
            result = EntityUtils.toString(response.getEntity(), "utf-8");
        }catch (Exception e){
            e.printStackTrace();
//            log.info("请求URL失败:"+url + "  code:" +codeStatus);
        }finally {
            return result;
        }

    }

    public static void main(String[] args) {
        System.out.println(" http://123.56.137.232:8081/api/userBankFirst?data={%22userId%22:1}&service=userBankFirst&version=1.0&platform=FENSEBOOK".substring(50));
    }
}